package cn.otrue.patienthealthmanager.fragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.activity.DoctorParticularsActivity;
import cn.otrue.patienthealthmanager.adapter.FindDoctorAllFragmentAdpater;
import cn.otrue.patienthealthmanager.bean.DoctorBaseinfo;
import cn.otrue.patienthealthmanager.bean.TransferParameter;
import cn.otrue.patienthealthmanager.http.HttpCons;
import cn.otrue.patienthealthmanager.http.HttpServer;
import cn.otrue.patienthealthmanager.http.ServerHandler;
import cn.otrue.patienthealthmanager.utils.MyConst;
import cn.otrue.patienthealthmanager.view.SwitchButtonListClickListener;
import cn.otrue.patienthealthmanager.view.SwitchListButton;
import cn.otrue.patienthealthmanager.view.XListView;
import cn.otrue.patienthealthmanager.view.XListView.IXListViewListener;

import com.google.gson.reflect.TypeToken;

/**
 * 找医生->全部医生
 * 
 * @author G
 * 
 */
public class FindDoctorAllFragment extends BaseFragment implements
		OnTouchListener, IXListViewListener {
	public static int isRefresh = 0;
	private List<DoctorBaseinfo> allDoctor = new ArrayList<DoctorBaseinfo>();
	private View rootView;
	private XListView lw_all;
	private int currentPage = 1;
	private FindDoctorAllFragmentAdpater adpater;
	public int type;
	private Handler handler = new ServerHandler() {

		@Override
		public void onSuccess(Bundle data, String state, String msg) {
			onLoad();
			if (state.equals("001")) {
				String dataStr = data.getString("data");
				List<DoctorBaseinfo> newList = HttpServer.gson.fromJson(
						dataStr, new TypeToken<List<DoctorBaseinfo>>() {
						}.getType());
				int uuu = allDoctor.size();
				allDoctor.addAll(newList);
				if (allDoctor.size() > 5) {
					lw_all.setPullLoadEnable(true);

				}
				Log.e("allDoctor", allDoctor.size() + "");
				adpater = new FindDoctorAllFragmentAdpater(
						rootView.getContext(), allDoctor, MyConst.ALL_DOCTOR);
				Toast.makeText(FindDoctorAllFragment.this.getActivity(), msg, 0)
						.show();
				lw_all.setAdapter(adpater);
				lw_all.setSelection(uuu);
			} else {
				onLoad();
			}
		}

		@Override
		public void onFail(Bundle data, String state) {
			onLoad();
		}

	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		rootView = inflater.inflate(R.layout.fragment_doctor_all, null);
		initView(rootView);
		return rootView;
	}

	private void initView(final View rootView) {

		// 初始化
		slb_all_doctor = (SwitchListButton) rootView
				.findViewById(R.id.slb_all_doctor);
		// 设置显示的文字
		String[] arr = { "地域", "好评", "活跃度", "星级", "价格" };
		// 设置每个textView的宽高
		// 初始化 （数据,宽,高,padding集合(默认为null),点击监听）
		slb_all_doctor.initButton(Arrays.asList(arr), 65, 35, null,
				new SwitchButtonListClickListener() {

					@Override
					public void onGetClicked(int cid) {
						Log.e("Switch", cid + "");
						allDoctor.clear();
						type = cid;
						currentPage = 0;
						getDate();

					}
				});
		// 设置初始的显示的数值
		slb_all_doctor.setClicked(0);
		lw_all = (XListView) rootView.findViewById(R.id.lw_all);
		lw_all.setPullLoadEnable(false); // 是否开启上拉加载
		lw_all.setXListViewListener(this);// 上啦加载和下拉刷新的监听
		lw_all.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {

				Intent intent = new Intent();
				intent.putExtra("aliasAccount", allDoctor.get(arg2 - 1)
						.getAliasAccount());
				intent.putExtra("jid", allDoctor.get(arg2 - 1).getJid());
				intent.putExtra("isAttention", allDoctor.get(arg2 - 1)
						.getIsAttention());

				intent.setClass(rootView.getContext(),
						DoctorParticularsActivity.class);

				startActivity(intent);

			}
		});
		adpater = new FindDoctorAllFragmentAdpater(rootView.getContext(),
				allDoctor, MyConst.ALL_DOCTOR);

		lw_all.setAdapter(adpater);
		getDate();
	}

	/** 地域 */
	public static final int Regional = 0;
	/** 好评 */
	public static final int FEEDBACK_RATE = 1;
	/** 活跃度 */
	public static final int ACTIVITY = 2;
	/** 星级 */
	public static final int STAR = 3;
	/** 价格 */
	public static final int PRICE = 4;

	private void getDate() {

		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("mobile", "");
		map.put("realname", "");
		if (TextUtils.isEmpty(MyConst.getACCOUNT())) {
			map.put("sender", "");
		} else {
			//map.put("sender", MyConst.getACCOUNT());
		}
		map.put("sender", "");//临时方案
		switch (type) {
		case Regional:

			break;
		case FEEDBACK_RATE:
			map.put("feedbackrate", "feedbackrate");
			break;
		case ACTIVITY:
			map.put("activity", "activity");
			break;
		case STAR:
			map.put("star", "star");
			break;
		case PRICE:
			map.put("priceForPoint", "priceForPoint");

			break;
		default:
			break;
		}

		TransferParameter parameter = new TransferParameter();
		parameter.setPagesize("20");
		parameter.setCurrentPage(currentPage + "");
		parameter.setData(map);
		new HttpServer().post(HttpCons.QUERY_DOCTOR_LIST, parameter, handler);

	}

	private void onLoad() {
		lw_all.stopRefresh();
		lw_all.stopLoadMore();
		lw_all.setRefreshTime("刚刚");
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		lw_all.setPullLoadEnable(false);
		allDoctor.clear();
		getDate();
		super.onResume();
	}

	private SwitchListButton slb_all_doctor;

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onTouch(View arg0, MotionEvent arg1) {
		// TODO Auto-generated method stub
		return false;
	}

	public static Fragment newInstance() {
		return new FindDoctorAllFragment();
	}

	@Override
	public String setPageName() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		allDoctor.clear();
		currentPage = 1;

		getDate();
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		currentPage++;

		getDate();
	}

	int i = 0;

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
		if (isVisibleToUser) {
			// 相当于Fragment的onResume
			if (!TextUtils.isEmpty(MyConst.getACCOUNT())) {
				if (isRefresh == 1) {
					allDoctor.clear();
					currentPage = 1;
					getDate();
					isRefresh = 0;
				}
			}

		} else {
			// 相当于Fragment的onPause

		}
	}
}
